<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import{ userInfoService } from '@/api/user.js'
//导入用户信息状态
import { useUserInfoStore } from '@/stores/userInfo.js';
//导入pinia
const userInfoStore = useUserInfoStore();
import {ref} from 'vue'
// <!-- 调用函数,获取用户信息 -->

 const getUserInfo= async()=>{
    //调用接口
    let res = await userInfoService();
    //存储pinia
    userInfoStore.info =res.data;
 }
 //调用函数
 getUserInfo();
 //条目被点击
 //导入路由
 import { useRouter } from 'vue-router';
 import {ElMessage,ElMessageBox} from 'element-plus'
 const router = useRouter();
 const handleCommand = (command)=>{  //用户信息跳转
    //判断指令
    if(command==='logout'){
       /*  //退出登录
        alert('退出登录')
        // //清空用户信息
        userInfoStore.info = {};
        //跳转到登录页面
        router.push('/login'); */
         //退出登录
         ElMessageBox.confirm(
            '你确认退出登录码？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //用户点击了确认
                //清空pinia中的token和个人信息
                userInfoStore.info={}
                tokenStore.token=''
                //跳转到登录页
                router.push('/login')
            })
            .catch(() => {
                //用户点击了取消
                ElMessage({
                    type: 'info',
                    message: '取消退出',
                })
            })
    }else{
        //跳转到对应的页面
        router.push('/user/'+command);
    }
    
 }
</script>





<template>
    <!--container  element-plus中的容器组件，默认是横向的，所以要设置direction="vertical"，才能实现纵向的布局-->

    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!--  element-plus中的菜单标签 -->
            <el-menu active-text-color="#ffd04b" background-color="#55a6e9"  text-color="#fff"
                router>
                <!-- 为菜单项 el-menu-item 设置index属性，设置点击后的路由路径 -->
                <el-menu-item  index="/article/category" >
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>文章分类</span>
                </el-menu-item>
                <el-menu-item index="/article/manage">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>文章管理</span>
                    
                </el-menu-item>


                
                <el-sub-menu index="1" >
                    <template #title>
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>热点消息</span>
                </template>
                 <!-- 添加子菜单项 -->
                 <el-menu-item index="/article/news">
                        <span>头条新闻</span>
                    </el-menu-item>
                    <el-menu-item >
                        <span>本地新闻</span>
                    </el-menu-item>
                </el-sub-menu>



                <!-- 商城 -->
                <el-sub-menu index="shop" >
                    <template #title>
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>商城</span>
                </template>
                 <el-menu-item index="/shop/index">
                        <span>主页</span>
                    </el-menu-item>
                    <el-menu-item index="">
                        <span>本地新闻</span>
                    </el-menu-item>
                </el-sub-menu>
                <!-- 子菜单 -->
                <el-sub-menu index="2">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item  index="/user/resetPassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>当前用户=><strong>{{ userInfoStore.info.nickname ? userInfoStore.info.nickname : userInfoStore.info.usrename }}</strong></div>
                <!-- 下拉菜单 -->
                <el-dropdown placement="bottom-end" @command="handleCommand" >
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <!-- 在el-dropdown标签上绑定command事件,当有条目被点击后，会触发这个事件 -->
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- border 边框 -->
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <router-view> </router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>大事件 ©2023 Created by sunyanhu</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #55a6e9;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>